﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using IA2NeuralNetwork.CharMap;

namespace IA2NeuralNetwork
{
    public partial class FormDefault : Form
    {
        private List<CheckBox> CharInputCbx;

        public FormDefault()
        {
            InitializeComponent();
            this.CharInputCbx = new List<CheckBox>();
            this.PopulateCharInputCbx();
            this.EventCharInputCbx();
        }

        #region CharInput

        private void PopulateCharInputCbx()
        {
            this.CharInputCbx.Clear();
            this.CharInputCbx.Add(this.cbx0);
            this.CharInputCbx.Add(this.cbx1);
            this.CharInputCbx.Add(this.cbx2);
            this.CharInputCbx.Add(this.cbx3);
            this.CharInputCbx.Add(this.cbx4);
            this.CharInputCbx.Add(this.cbx5);
            this.CharInputCbx.Add(this.cbx6);
            this.CharInputCbx.Add(this.cbx7);
            this.CharInputCbx.Add(this.cbx8);
            this.CharInputCbx.Add(this.cbx9);
            this.CharInputCbx.Add(this.cbx10);
            this.CharInputCbx.Add(this.cbx11);
            this.CharInputCbx.Add(this.cbx12);
            this.CharInputCbx.Add(this.cbx13);
            this.CharInputCbx.Add(this.cbx14);
            this.CharInputCbx.Add(this.cbx15);
            this.CharInputCbx.Add(this.cbx16);
            this.CharInputCbx.Add(this.cbx17);
            this.CharInputCbx.Add(this.cbx18);
            this.CharInputCbx.Add(this.cbx19);
            this.CharInputCbx.Add(this.cbx20);
            this.CharInputCbx.Add(this.cbx21);
            this.CharInputCbx.Add(this.cbx22);
            this.CharInputCbx.Add(this.cbx23);
            this.CharInputCbx.Add(this.cbx24);
            this.CharInputCbx.Add(this.cbx25);
            this.CharInputCbx.Add(this.cbx26);
            this.CharInputCbx.Add(this.cbx27);
            this.CharInputCbx.Add(this.cbx28);
            this.CharInputCbx.Add(this.cbx29);
            this.CharInputCbx.Add(this.cbx30);
            this.CharInputCbx.Add(this.cbx31);
            this.CharInputCbx.Add(this.cbx32);
            this.CharInputCbx.Add(this.cbx33);
            this.CharInputCbx.Add(this.cbx34);
            this.CharInputCbx.Add(this.cbx35);
            this.CharInputCbx.Add(this.cbx36);
            this.CharInputCbx.Add(this.cbx37);
            this.CharInputCbx.Add(this.cbx38);
            this.CharInputCbx.Add(this.cbx39);
            this.CharInputCbx.Add(this.cbx40);
            this.CharInputCbx.Add(this.cbx41);
            this.CharInputCbx.Add(this.cbx42);
            this.CharInputCbx.Add(this.cbx43);
            this.CharInputCbx.Add(this.cbx44);
            this.CharInputCbx.Add(this.cbx45);
            this.CharInputCbx.Add(this.cbx46);
            this.CharInputCbx.Add(this.cbx47);
        }

        private void EventCharInputCbx()
        {
            int charInputLength = this.CharInputCbx.Count();

            for (int i = 0; i < charInputLength; i++)
            {
                this.CharInputCbx[i].Click += this.cbx_Click;
            }
        }

        private void SetCharInput(byte[] positions)
        {
            int pLength = positions.Length;
            if (positions.Length == this.CharInputCbx.Count())
            {
                for (int i = 0; i < pLength; i++)
                {
                    this.CharInputCbx[i].Checked = (positions[i] == 0) ? false : true;
                }
            }
        }

        private byte[] GetCharInput()
        {
            int charInputLength = this.CharInputCbx.Count();
            byte[] positions = new byte[charInputLength];

            for (int i = 0; i < charInputLength; i++)
            {
                positions[i] = (byte) ( (this.CharInputCbx[i].Checked == true) ? 1 : 0 );
            }

            return positions;
        }

        private void CheckCharInput()
        {
            int charInputLength = this.CharInputCbx.Count();

            for (int i = 0; i < charInputLength; i++)
            {
                this.CharInputCbx[i].Checked = true;
            }
        }

        private void UncheckCharInput()
        {
            int charInputLength = this.CharInputCbx.Count();

            for (int i = 0; i < charInputLength; i++)
            {
                this.CharInputCbx[i].Checked = false;
            }
        }

        private void cbx_Click(object sender, EventArgs e)
        {
            this.DebugCharInput();
        }

        private void btnCharInput_Click(object sender, EventArgs e)
        {
            try
            {
                CharRaw cr = new CharRaw(this.txtCharInput.Text.Substring(0, 1));

                CharByte cb = (CharByte)cr;
                this.SetCharInput(cb.Data);

                CharLabel cl = (CharLabel)cr;
                this.lblLabel.Text = String.Join("", cl.Data);
            }
            catch (Exception)
            {
                this.UncheckCharInput();
                this.lblLabel.Text = "------";
            }
            this.DebugCharInput();
        }

        private void DebugCharInput()
        {
            this.txtOutput.AppendText("[Checkbox Values]");
            this.txtOutput.AppendText(Environment.NewLine);
            this.txtOutput.AppendText(String.Join(", ", this.GetCharInput()));
            this.txtOutput.AppendText(Environment.NewLine);
        }

        #endregion
    }
}
